<link href="<?php echo Yii::app()->theme->baseUrl ?>/css/jquery-ui.css" type="text/css" rel="stylesheet">
<script src="<?php echo Yii::app()->theme->baseUrl ?>/js/autoNumeric.js"></script>
<script src="<?php echo Yii::app()->theme->baseUrl ?>/js/bootstrap-tokenfield/bootstrap-tokenfield.js"></script>
   <link href="<?php echo Yii::app()->theme->baseUrl ?>/js/bootstrap-tokenfield/css/tokenfield-typeahead.css" type="text/css" rel="stylesheet">
    <link href="<?php echo Yii::app()->theme->baseUrl ?>/js/bootstrap-tokenfield/css/bootstrap-tokenfield.css" type="text/css" rel="stylesheet">
    <link rel="stylesheet" href="<?php echo Yii::app()->theme->baseUrl ?>/js/datetimepicker/jquery.datetimepicker.css">
  <script src="<?php echo Yii::app()->theme->baseUrl ?>/js/datetimepicker/jquery.datetimepicker.js"></script>
<div class="col-md-12">
    <div class="box box-primary">
        <div class="box-header">
            
        </div><!-- /.box-header -->
        <div class="box-body">
            <h1 class="box-title">Thanh toán của đối tác: <span class="name-ten"><?php echo $model->ten; ?></span></h1>
            <dl class="dl-horizontal" style="margin-top: 32px;">
                <div id="div_nhap">
                </div>
                <dd class="pull-left" style="margin-left: 0px;">
                    <p id="info"></p>
                    <a id="save_hoadon" class="btn btn-primary">Chọn thanh toán</a><br><br>
                    <input type="text" id="date1">
                    đến
                    <input type="text" id="date2">
                    <button id="loc">Lọc</button>
                </dd>
                 <div class="pull-right" style="margin-bottom:10px;">
                    <?php echo CHtml::image("images/trangthai-0.png", "DORE",array("class"=>"image_view")); ?><label>Chưa thanh toán</label>
                    <?php echo CHtml::image("images/trangthai-1.png", "DORE",array("class"=>"image_view")); ?><label>Thanh toán một phần</label>
                    <?php echo CHtml::image("images/trangthai-2.png", "DORE",array("class"=>"image_view")); ?><label>Đã thanh toán</label>
                </div>
                <div style="clear:both;"></div>
            </dl>
           <?php $this->widget('ext.GLGridView', array(
                'id'=>'doitac-grid1',
                'cssFile' => Yii::app()->theme->baseUrl . '/css/gridView.css',
                'dataProvider'=>$data,
                'afterAjaxUpdate'=>'js:function(){
                      $("input[type=checkbox]:not(.simple)").iCheck({
                        checkboxClass: "icheckbox_minimal",
                        radioClass: "iradio_minimal"
                    });
                    Start();
                }',
                'columns'=>array(
                    array(
                        'name'=>'id',
                        'type'=> 'raw',
                        'value'=> 'CHtml::hiddenField("id", $data->id,array("class"=>"idhoadon")).$data->id', 
                    ),
                    array(
                        'name'=>'id_khachhang',
                        'htmlOptions'=>array('class'=>'name-ten'),
                        'value'=> '$data->khachhang == null ? "Không có" : $data->khachhang->ten',
                    ),
                    array(
                        'name'=>'id_doitac',
                        'htmlOptions'=>array('class'=>'name-ten'),
                        'value'=> '$data->doitac == null ? "Không có" : $data->doitac->ten',
                    ),
                    array(
                        'name'=>'Mã số đối tác',
                        'value'=>'$data->doitac == null ? "Không có" : $data->doitac->ma_so',
                        'htmlOptions'=>array('class'=>'ma-doitac'),
                    ),
                    'so_phieu_thanh_toan',
                    array(
                        'name'=>'so_tien',
                        'value'=>'getMoney($data->so_tien)'
                    ),
                    array(
                        'name'=>'chua_thanh_toan_dt',
                        'value'=>'getMoney($data->chua_thanh_toan_dt)'
                    ),
                    array(
                        'name'=>'chiet_khau',
                        'value'=>'getMoney($data->chiet_khau)'
                    ),
                    'ngay_tao',
                    'ngay_su_dung',
                    array(
                        'name'=>'ngay_thanh_toan_dt',
                        'value'=>'($data->ngay_thanh_toan_dt == "0000-00-00" || $data->ngay_thanh_toan_dt == null) ? "N/A" : $data->ngay_thanh_toan_dt'
                    ),
                    array(
                        'name'=>'trangthai_thanhtoan_dt',
                        'type'=>'raw',
                        'value'=> 'CHtml::image("images/trangthai-{$data->trangthai_thanhtoan_dt}.png", "DORE",array("class"=>"image"));', 
                    ),
                    array(
                        'class'=>'CCheckBoxColumn',
                        'selectableRows' => 2,   
                        'headerTemplate'=>'Thanh toán {item}',
                        'id'=>'selectedItems',
                        'cssClassExpression'=>'DoitacKhachhang::TrangThaiDoitac($data) == 2 ? "disable" : ""',
                        'htmlOptions'=>array('class'=>"input_checkbox")
                    ),
                ),
            )); ?> 
            
            <div class="box-footer" style="margin-top: 35px;">
                <a href="<?php echo Yii::app()->createUrl('/DoiTac/doitacKhachhang/doitac',array('id'=>$model->id)); ?>" class="btn btn-danger">Xem lịch sử</a>
            </div>
        </div><!-- /.box-body -->
        
    </div><!-- /.box -->
</div>
<script type="text/javascript">
    $( "#date1" ).datepicker();
    $( "#date2" ).datepicker();
    $('body').on('click', '#loc', function(event) {
        if ($('#date1').val() != '' && $('#date2').val() != '') {
            var date1 = $('#date1').val();
            var date2 = $('#date2').val();
            $.fn.yiiGridView.update('doitac-grid1',{data: {date1:date1,date2:date2}});
        };
    }); 
</script>
<style type="text/css">
.input_text{
    display: inline-block;
    width: 50%;
}
.form-group label{
    width: 140px;
}
.box-thanhtoan{
    width:328px;
    margin-top: 60px;
    margin-left: 140px;
}
#div_nhap{
    display: none;
}
.nut_thanhtoan{
    float: right;
}
#div_nhap{
    margin: -22px 2px;
}
</style>


<script type="text/javascript">
       function Start () {
           $('.disable input').iCheck('disable');
            var checkAll = $("input[type=checkbox]#selectedItems_all");
            var checkboxes = $("input[type=checkbox]:not(:disabled)").not("#selectedItems_all");
            checkAll.on("ifChecked ifUnchecked", function(event) {
                if (event.type == "ifChecked") {
                    checkboxes.iCheck("check");
                } else {
                    checkboxes.iCheck("uncheck");
                }
            });

            checkboxes.on("ifChanged", function(event){
                if(checkboxes.filter(":checked").length == checkboxes.length) {
                    checkAll.prop("checked", "checked");
                } else {
                    checkAll.removeProp("checked");
                }
                checkAll.iCheck("update");
            });
       }
        
        $(function () {
            Start();
        });
        $("#save_hoadon").click(function() {
            var  arrs = [];
            $.each($("#doitac-grid1 input[type=checkbox]:checked").not("#selectedItems_all"), function(index, val) {
                 arrs.push($(this).parent().parent().parent().find('.idhoadon').val());
            });
            if (arrs.length > 0) {
                $.ajax({
                    url: "<?php echo Yii::app()->createUrl('/DoiTac/default/viewthanhtoan'); ?>",
                    type: 'POST',
                    data: {id:<?php echo $model->id; ?>,data:JSON.stringify(arrs)},
                    success: function (data) {
                        $('#div_nhap').html(data);
                        $('#div_nhap').fadeIn();
                        $('#save_hoadon').hide();
                        setTimeout(function() {
                        }, 2000);
                    }
                });
            }else{
                $('#info').html("Chưa chọn hóa đơn");
                setTimeout(function() {
                    $('#info').html("");
                }, 2000);
            }
        });
</script>